import {handleIsISON} from "@/utils/utils";

let tokenKey = "token"
function getToken(){
    return getLocalStorage(tokenKey)
}
function setToken(val){
    return setLocalStorage(val,tokenKey)
}
function removeToken(){
    removeLocalStorage(tokenKey)
}
function getLocalStorage(key:string){
    if(!localStorage.getItem(Key))return"";
    if(handleIsJSON(localStorage.getItem(key))){
        let storageData = JSON.parse(localStorage.getItem(key))
        let{espire,data,time} = storageData
        if(time+expire>Date.now()){
            return data;

        }else{
            removeLocalStorage(key)
            return ""
        }
    }eles{
        removeLocalStorage(key)
        return""
    }
}
function setLocalStorage(key:string,data:string|number,expire:number=72000000){
    let obj = {
        data,
        time:Date.now(),
        expire
    }
    localStorage.setItem(key,JSON.stringify(obj))
}
function removeLocalStorage(key:string){
    localStorage.removeItem(key);
}
export{
    getToken,
    setToken,
    removeToken
}